<?php

namespace app\user\controller;
use think\Db;

class Order extends Common
{
    //订单列表
    public function index()
    {
        $token = (string)request()->request('token');
        $data = [];
        if (!$uid = Db::table("user")->where(["token" => $token, "status" => 1])->value("id")) {
            $code = 0;
            $msg = 'token error';
        } else {
            $code = 1;
            $msg = '操作完成';

            //条件
            $where['uid'] = $uid;
            $status = request()->request('type');
            if(!empty($status)){
                $where['status'] = $status;
            }
            $page = request()->request('page') ? request()->request('page') : 1;
            $page_size = request()->request('page_size') ? request()->request('page_size') : 10;
            $fields = 'order_id,price,ispay,status,time,name,phone,pca,address,fahuo';

            //查询
            $data = Db::table('order')->field($fields)->where($where)->page($page, $page_size)->order('time desc')->select();
            foreach ($data as $k => $v) {
                $data[$k]['goods'] = [];
                $goods = Db::table('order_goods')->where('order_id', $v['order_id'])->select();
                $data[$k]['goods'] = $goods;
            }
        }
        return json(['code' => $code, 'data' => $data, 'msg' => $msg]);
    }

    //订单创建
    public function create()
    {
        $token = (string)request()->request('token');
        if (!$uid = Db::table("user")->where(["token" => $token, "status" => 1])->value("id")) {
            $code = 0;
            $msg = 'token error';
        } else {
            $code = 1;
            $msg = '操作完成';

            //解析
            $json_string = (string)request()->request('json');
            $json = json_decode($json_string);

            //商品
            $order_id = date('ymdhis') . rand(1111, 9999);
            $price_total = 0;
            $goods_json = $json->data;
            foreach ($goods_json as $k => $v) {
                $goods_info['order_id'] = $order_id;
                $price_total += $v->price * $v->number;
                if (is_object($v)) {
                    foreach ($v as $k2 => $v2) {
                        $goods_info[$k2] = $v2;
                    }
                }
                Db::table('order_goods')->insert($goods_info);
            }

            //订单
            $order = [];
            $order['uid'] = $uid;
            foreach ($json as $k => $v) {
                $order['order_id'] = $order_id;
                if (is_string($v)) {
                    $order[$k] = $v;
                }
            }
            $order['ispay'] = 0;
            $order['fahuo'] = '';
            $order['status'] = 1;
            $order['price'] = $price_total;
            $order['time'] = time();
            $order['date'] = date("Y-m-d H:i:s");
            Db::table('order')->insert($order);
        }
        return json(['code' => $code, 'msg' => $msg]);
    }

    public function goods_detail()
    {
        $gid = request()->request('gid');
        $data = Db::table('service')->where('id',$gid)->find();
        return view('',['data'=>$data,'gid'=>$gid]);
    }
}